import axios from 'axios'
const login = {
  state: {
    token: JSON.parse(localStorage.getItem('state')).login.token || '',
  },
  mutations: {
    set_token(state, data) {
      state.token = data
      console.log('vuex中接收到的参数:', data)
    },
    set_list(state, item) {
      state.list.push(item)
    },
  },
  actions: {
    async getLogin({ commit }, data) {
      try {
        const res = await axios.post('/api/login', data)
        if (res.data.code === 0) {
          commit('set_token', res.data.data.token)
          return { code: 0, msg: '登陆成功' }
        } else {
          return { code: -1, msg: '账号密码错误' }
        }
      } catch (err) {
        return { code: -1, msg: err.message }
      }
    },
  },
}

export default login
